﻿$(document).ready(function () {
    var report_tf = false;
    var boxContent = $("#replyBox2").html();
    var returnUrl = $("#returnUrl").val();
    //评论
    $(".replay_btn").click(function () {
        var fid = $(this).parents(".replay_bu").children(".fid").val();
        $("#replyBox2").html("");
        $(".replyBoxReady").html("");
        $(this).parents(".reply_list_item").children(".replyBoxReady").html(boxContent);
        $("#fid").val(fid);
        $("#btnReplySub2").click(function () {
            var node = $(this).parents(".replyBoxReady").parents(".reply_list_item").children(".reply_list_con").children(".replay_bu");
            var content = $("#replyContent").val();
            var cid = $("#cuserId").val();
            var id = $("#topicid").val();
            var fatherID = $("#fid").val();
            if (cid == "") {
                CastMsg("请先登录，才可以添加评论！");
                setTimeout(function () {
                    location.href = "/Login?returnUrl=" + returnUrl;
                }, 2000);
                return false;
            }
            else {
                if (content == "") {
                    CastMsg("无评论内容，请重新评论！")
                    return false;
                }
                $.ajax({
                    type: "post",
                    url: "/Bbs/ReplyAdd",
                    data: { content: content, cid: cid, id: id, fatherID: fatherID }
                }).done(function (data) {
                    console.log(data);
                    if (data.Statu == "ok") {
                        CastMsg("评论成功");
                        ue.setContent("我想说点什么...");
                        var str = "";
                        if (fatherID != "") {
                            str += '<div class="reply_talk clearfix"><span class="frameem"></span><i class="triangle"></i><ul class="clearfix">';
                            str += '<li class="clearfix" data-value="1452169"><div><a href="/User/PersonalShow/' + data.Data.User.ID + '" class="user_name" target="_blank">' + data.Data.User.UserName + '</a>';
                            str += '<i class="vip_icon "></i>' + data.Data.Content + '<span class="time">' + moment(data.Data.Time).format('YYYY-MM-DD HH:mm:ss') + '</span></div></li></ul></div>';
                            node.before(str);
                            $(".replyBoxReady").html("");
                        }
                        else {
                            if (data.Data.User.Picture == null) {
                                str += '<li class="reply_list_item clearfix" post-id="369651146" id="1" float-id="1" u-id="78540183"> <div class="reply_list_img">';
                                str += '<img src="/Images/userphoto.png" class="user_head" />';
                                str += '</div> <div class="reply_list_con"><div class="auth_msg clearfix"> <a href="/User/PersonalShow/' + data.Data.User.ID + '" class="auth_name" target="_blank"> ' + data.Data.User.UserName + ' </a> <span class="at"> 发表于</span> <span class="time">' + moment(data.Data.Time).format('YYYY-MM-DD hh:mm:ss') + '</span> <a class="only_read_auth">只看该作者</a> </div> <div class="reply_txt"> <p> <span style="color: rgb(51, 51, 51);font-family: arial, Microsoft Yahei, Hiragino Sans GB, sans-serif; line-height: 28px;background-color: rgb(255, 255, 255);"> ' + data.Data.Content + ' </span> </p> </div> <p class="replay_bu"> <a href="#" class="replay_btn">回复</a> </p> </div>';
                                $("#reply").prepend(str);
                            }
                            else {
                                str += '<li class="reply_list_item clearfix" post-id="369651146" id="1" float-id="1" u-id="78540183"> <div class="reply_list_img">';
                                str += '<img src="/User/ShowPicture/' + data.Data.User.ID + '" class="user_head" />';
                                str += '</div> <div class="reply_list_con"><div class="auth_msg clearfix"> <a href="/User/PersonalShow/' + data.Data.User.ID + '" class="auth_name" target="_blank"> ' + data.Data.User.UserName + ' </a> <span class="at"> 发表于</span> <span class="time">' + moment(data.Data.Time).format('YYYY-MM-DD hh:mm:ss') + '</span> <a class="only_read_auth">只看该作者</a> </div> <div class="reply_txt"> <p> <span style="color: rgb(51, 51, 51);font-family: arial, Microsoft Yahei, Hiragino Sans GB, sans-serif; line-height: 28px;background-color: rgb(255, 255, 255);"> ' + data.Data.Content + ' </span> </p> </div> <p class="replay_bu"> <a href="#" class="replay_btn">回复</a> </p> </div>';
                                $("#reply").prepend(str);
                            }
                        }
                    }
                    else {
                        CastMsg("评论失败");
                    }

                });
            }

        });
    });
    //回复评论
    $("#btnReplySub").click(function () {
        var content = ue.getContent();
        var cid = $("#cuserId").val();
        var id = $("#topicid").val();
        var fatherID = "";
        if (cid == "") {
            CastMsg("请先登录，才可以回复评论！");
            setTimeout(function () {
                location.href = "/Login?returnUrl=" + returnUrl;
            }, 2000);
            return false;
        }
        else {
            if (content == "") {
                CastMsg("无评论内容，请重新评论！")
                return false;
            }
            $.ajax({
                type: "post",
                url: "/Bbs/ReplyAdd",
                data: { content: content, cid: cid, id: id, fatherID: fatherID }
            }).done(function (data) {
                if (data.Statu == "ok") {
                    CastMsg("评论成功");
                    ue.setContent("我想说点什么...");
                    var str = "";
                    if (fatherID != "") {
                        str += ' <div class="reply_talk clearfix"><span class="frameem"></span><i class="triangle"></i><ul class="clearfix">';
                        str += '<li class="clearfix" data-value="1452169">div><a href="/User/PersonalShow/' + data.Data.User.ID + '" class="user_name" target="_blank">' + data.Data.User.UserName + '</a>';
                        str += '<i class="vip_icon "></i>' + data.Data.Content + '<span class="time">' + moment(data.Date.Time).format('YYYY-MM-DD HH:mm:ss') + '</span></div></li></ul> </div>';
                        var node = $(this).parents(".replyBoxReady").parents(".reply_list_item").children(".reply_list_con").children(".replay_bu").prepend(str);
                    }
                    else {
                        if (data.Data.User.Picture == null) {
                            str += '<li class="reply_list_item clearfix" post-id="369651146" id="1" float-id="1" u-id="78540183"> <div class="reply_list_img">';
                            str += '<img src="/Images/userphoto.png" class="user_head" />';
                            str += '</div> <div class="reply_list_con"><div class="auth_msg clearfix"> <a href="/User/PersonalShow/' + data.Data.User.ID + '" class="auth_name" target="_blank"> ' + data.Data.User.UserName + ' </a> <span class="at"> 发表于</span> <span class="time">' + moment(data.Data.Time).format('YYYY-MM-DD hh:mm:ss') + '</span> <a class="only_read_auth">只看该作者</a> </div> <div class="reply_txt"> <p> <span style="color: rgb(51, 51, 51);font-family: arial, Microsoft Yahei, Hiragino Sans GB, sans-serif; line-height: 28px;background-color: rgb(255, 255, 255);"> ' + data.Data.Content + ' </span> </p> </div> <p class="replay_bu"> <a href="#" class="replay_btn">回复</a> </p> </div>';
                            $("#reply").prepend(str);
                        }
                        else {
                            str += '<li class="reply_list_item clearfix" post-id="369651146" id="1" float-id="1" u-id="78540183"> <div class="reply_list_img">';
                            str += '<img src="/User/ShowPicture/' + data.Data.User.ID + '" class="user_head" />';
                            str += '</div> <div class="reply_list_con"><div class="auth_msg clearfix"> <a href="/User/PersonalShow/' + data.Data.User.ID + '" class="auth_name" target="_blank"> ' + data.Data.User.UserName + ' </a> <span class="at"> 发表于</span> <span class="time">' + moment(data.Data.Time).format('YYYY-MM-DD hh:mm:ss') + '</span> <a class="only_read_auth">只看该作者</a> </div> <div class="reply_txt"> <p> <span style="color: rgb(51, 51, 51);font-family: arial, Microsoft Yahei, Hiragino Sans GB, sans-serif; line-height: 28px;background-color: rgb(255, 255, 255);"> ' + data.Data.Content + ' </span> </p> </div> <p class="replay_bu"> <a href="#" class="replay_btn">回复</a> </p> </div>';
                            $("#reply").prepend(str);
                        }
                    }
                }
                else {
                    CastMsg("评论失败");
                }
            });
        }

    });
    //板块推荐和板块列表切换
    $("#tab_span_1").mouseover(function () {
        $("#tab_span_2").removeClass("on");
        $("#tab_ul_2").removeClass("on");
        $("#tab_span_1").addClass("on");
        $("#tab_ul_1").addClass("on");
    });
    $("#tab_span_2").mouseover(function () {
        $("#tab_span_1").removeClass("on");
        $("#tab_ul_1").removeClass("on");
        $("#tab_span_2").addClass("on");
        $("#tab_ul_2").addClass("on");
    });
    //增加主题
    $("#go_add_theme").click(function () {
        var rid = $("#cuserId").val();
        var pid=$("#plateID").val();
        if (rid == "") {
            CastMsg("请先登录，才可以添加主题！");
            setTimeout(function () {
                location.href = "/Login?returnUrl=" + returnUrl;
            }, 2000);
        }
        else {
            if(pid!=null)
            {
                location.href = "/Bbs/Add/" + pid;
            }
            else
            {
                location.href = "/Bbs/Add/";
            }
        }
    });
    ///签到
    $("#JD_sign").click(function () {
        var rid = $("#cuserId").val();
        if (rid == "") {
            CastMsg("请先登录，才可以签到！");
            setTimeout(function () {
                location.href = "/Login?returnUrl=" + returnUrl;
            }, 2000);
            return false;
        }
        else
        {
            var sid = $("#sid").val();
            $.post("/Bbs/Sign", { sid: sid }, function (data) {
                if (data.Statu == "ok") {
                    CastMsg("签到成功");
                    SignInfo();
                }
                else {
                    CastMsg(data.Msg);
                }
            });
        }
    });
    //举报
    $("#report").click(function () {
        if (report_tf == false)
        {
            var rid = $("#cuserId").val();
            if (rid == "") {
                CastMsg("请先登录，才可以添加举报！");
                setTimeout(function () {
                    location.href = "/Login?returnUrl=" + returnUrl;
                }, 2000);
            }
            else
            {
                var tid = $("#topicId").val();
                var sid = $("#sid").val();
                $.post("/Bbs/Report", { id: tid, sid: sid }, function (data) {
                    if (data.Statu == "ok") {
                        CastMsg("举报成功");
                        $("#report").html("已举报");
                        report_tf = true;
                    }
                    else {
                        CastMsg(data.Msg);
                    }
                });
            }
        }
        else
        {
            CastMsg("已举报，请勿重复操作");
        }
    });
    //关注
    $("#btnAddCollect").click(function () {
        var rid = $("#cuserId").val();
        if (rid == "") {
            CastMsg("请先登录，才可以关注！");
            setTimeout(function () {
                location.href = "/Login?returnUrl=" + returnUrl;
            }, 2000);
        }
        else
        {
            var plateId = $("#plateID").val();
            var sid = $("#sid").val();
            $.post("/Bbs/CollectForum", { sid: sid, id: plateId }, function (data) {
                if (data.Statu == "ok") {
                    CastMsg("关注成功！");
                    window.location.reload();
                }
                else {
                    CastMsg(data.Msg);
                }
            });
        }
    });
    //取消关注
     $("#btnCancelCollect").click(function () {
    var plateId = $("#plateID").val();
    var sid = $("#sid").val();
    $.post("/Bbs/CancelCollectForum", { sid: sid, id: plateId }, function (data) {
        if (data.Statu == "ok") {
            CastMsg("取消关注成功！");
            window.location.reload();
        }
        else {
            CastMsg(data.Msg);
        }
    });
});
    SignInfo();
});

function validateEditor() {
    var gcontent = UE.getEditor('container').getContentTxt();
    var content = UE.getEditor('container').hasContents();
    var posttitle = document.getElementById("posttitle");
    var catid = document.getElementById("catid");
    var user = $("#hdusername").val();
    if (posttitle.value == "") {
        CastMsg("请输入帖子标题！");
        return false;
    }
    if (catid.value == "") {
        CastMsg("请选择所属模块！");
        return false;
    }
    if (gcontent == "我想说点什么...") {
        CastMsg("请输入帖子内容！");
        UE.getEditor('container').focus();//光标返回编辑器中
        return false;
    }
    if (content == false) {
        CastMsg("请输入帖子内容！");
        UE.getEditor('container').focus();//光标返回编辑器中
        return false;
    }
    if (user == undefined) {
        CastMsg("请先登录，才可以发布！");
        setTimeout(function () {
            location.href = "/Login?returnUrl=" + returnUrl;
        }, 2000);
    }
    else {
        document.getElementById("myform").submit();
    }
}
function SignInfo() {
    $.ajax({
        type: "post",
        url: "/Bbs/SignInfo",
        success: function (data) {
            if (data.Statu == "ok") {
                $("#sign_all").html(data.Data[0] + "人");
                $("#sign_nums").html(data.Data[1] + "天");
                $("#sign_font").html("已签到");
                $("#sign_line").html("第"+data.Data[2]+"名");
                $("#JD_sign").addClass("visted");
            }
            else if (data.Statu == "only_num") {
                $("#sign_all").html(data.Data+"人");
                $("#sign_nums").html("0天");
                $("#sign_line").html("<span style='font-size:12px;'>签到看排名</span>");
                $("#JD_sign").removeClass("visted");
            }
            else if(data.Statu=="err"){
                $("#sign_all").html("0人");
                $("#sign_nums").html("0天");
                $("#sign_line").html("<span style='font-size:12px;'>签到看排名</span>");
                $("#JD_sign").removeClass("visted");
            }
            else
            {
                $("#sign_all").html("0人");
                $("#sign_nums").html("0天");
                $("#JD_sign").removeClass("visted");
            }
        },
        error: function (e) {
            CateMsg("网络异常");
            $("#JD_sign").removeClass("visted");
        }
    });
}